require "builder"
class ApiController < ApplicationController

  def get_dict

    un = request[:un]
    pwd = request[:pwd]
    act = request[:act]

    if un == nil || pwd == nil || act == nil
      redirect_to :controller => "error", :action => "error"
    end

    user = User.find_by_user_name(un)
    if !user || !user.password_matches?(pwd)
      redirect_to :controller => "error", :action => "error"
    end

    xml = Builder::XmlMarkup.new
    xml.instruct! :xml, :version => "1.0"
    words = Word.find_all_by_user_id(user.id)

    respond_to do |format|
      format.xml {
        render :text => xml.words {
          words.each do |word|
            xml.word {
              xml.original_word (word.original_word)
              word.translations.each do |trans|
                if trans.is_valid
                  xml.trans(trans.trans)
                else
                  xml.invalid_trans(trans.trans)
                end
              end
            }
          end
        }
      }
    end
  end

end